import store from '@/store' //vuex
import axios from 'axios'
const schedule = {
    calcRounds: (battlePublished, roundsSelected, teamsInfo) => {
        //let roundsDisabled = JSON.parse(JSON.stringify(roundsSelected))
        //判断已经发布的场地数 在轮次显示
        for (let row in battlePublished) {
            for (let col in battlePublished[row]) {
                if (battlePublished[row][col].list.length > 0) {
                    let battle = battlePublished[row][col].list[0]
                    let battleStage = parseInt(battle.stage) - 1
                    let battleWheel = parseInt(battle.wheel) - 1
                    let teamIndex = schedule.getTeamsIndex(battle, teamsInfo)
                        //let count = roundsSelected[battleStage][teamIndex][battleWheel].count
                    let battleArr = {}
                    let teamArr = []
                    let stageArr = []
                    stageArr = roundsSelected[battleStage]
                    teamArr = stageArr[teamIndex]
                    battleArr = teamArr[battleWheel]
                        // console.log(teamArr[battleWheel])
                    let obj = {}
                    if (!teamArr[battleWheel]) {
                        obj.count = ''
                        battleArr = obj
                    }
                    let count = battleArr.count
                    let countLength = battleArr.countLength
                    count < countLength ? ++battleArr.count : ''
                    if (battleArr.countLength == battleArr.count) {
                        battleArr.disable = true
                    }
                    //  console.log(battleArr)
                }
            }
        }
        //判断 轮次是否禁用
        // for(let team in teamsInfo){
        //   for(let stage in teamsInfo[team].roundInfo){
        //     for(let round in teamsInfo[team].roundInfo[stage]){
        //       console.log(teamsInfo[team].roundInfo[stage][round])
        //       let scoreCounts = 0
        //       for(let battle in teamsInfo[team].roundInfo[stage][round]){
        //         if(battle.score) ++scoreCounts
        //       }
        //       // console.log(team,stage,round,scoreCounts)
        //       // console.log(roundsSelected[stage][team][round])
        //       if(scoreCounts == roundsSelected[stage][team][round].countLength){
        //         roundsSelected[stage][team][round].disable = true
        //       }
        //     }
        //   }
        // }
        //store.commit('setRoundsSelected',roundsSelected)
        // console.log('schedule.js',roundsSelected)
        // console.log( `1111 ${store.state.platoonDate}`)
    },
    //战表 人名列表 回弹
    // battleReturnList:(battle,teamsInfo,roundsSelected,index,list)=>{
    //   let listIndex = schedule.battleReturnListIndex(battle,teamsInfo)
    //
    //   let stage  = listIndex.stage
    //   let team =  listIndex.teamIndex
    //   let round = listIndex.wheel
    //   // console.log(stage,team,round)
    //   // console.log(roundsSelected[stage][team])
    //   if(roundsSelected[stage][team][round].count > 0 ) roundsSelected[stage][team][round].count--
    //   let backArr = list.splice(index,1)
    //   let backItem = backArr[0]
    //   teamsInfo[team].rounds[stage][round].push(backItem)
    // },
    battleReturnListIndex: (battle, teamsInfo) => {
        let teamIndex = schedule.getTeamsIndex(battle, teamsInfo)
            // console.log(teamIndex)
            //  console.log(battle,teamsInfo)
            //teamsInfo[teamIndex].rounds[parseInt(battle.stage)-1][parseInt(battle.wheel)-1].status = 0
        let rounds = teamsInfo[teamIndex].rounds[parseInt(battle.stage) - 1][parseInt(battle.wheel) - 1]
        for (let item in rounds) {
            if (rounds[item].id == battle.id) {
                rounds[item].status = 0
            }
        }
        let stage = parseInt(battle.stage) - 1
        let wheel = parseInt(battle.wheel) - 1
            // console.log(stage,teamIndex,wheel)
        return { stage, teamIndex, wheel }
    },
    getTeamsIndex: (battle, teamsInfo) => {
        let teamIndex = 0
        for (let team in teamsInfo) {
            if (battle.group_id == teamsInfo[team].project_id) {
                teamIndex = team
            }
        }
        return parseInt(teamIndex)
    },
    Name: (battle, type, scoreDialog) => {

        let nameObj = {}
        if (type == 'a') {
            nameObj.site = battle.site_a
            nameObj.name = battle.name_a
            nameObj.one_limit = battle.one_limit_a
        }
        if (type == 'b') {
            nameObj.site = battle.site_b
            nameObj.name = battle.name_b
            nameObj.one_limit = battle.one_limit_b
        }
        let limit = ''
        let siteName = ''
        if (nameObj.one_limit) limit = `(${nameObj.one_limit})`
        if (nameObj.site == nameObj.name) {
            siteName = schedule.shortenName(nameObj.name)
        } else {
            if (scoreDialog == 1) siteName = schedule.shortenName(nameObj.site) + " " + schedule.shortenName(nameObj.name)
            if (scoreDialog != 1) siteName = schedule.shortenName(nameObj.site) + schedule.shortenName(nameObj.name)

        }
        if (scoreDialog == 1) return `${siteName}`
        return `${siteName}${limit}`
    },
    //1,2,3,4,5xx => 1-5
    shortenName: (name) => {
        let nameArr = []
        if (!name) return name
        nameArr = name.split(',')
        if (nameArr.length > 2) {
            return `${nameArr[0]}-${nameArr[nameArr.length-1]}`
        } else {
            return name
        }
    },
    wheelName: (battle) => {
        // let maxWheel = battle.wheels
        // let wheel = battle.wheel
        // let wheelName = ''
        // let field = battle.field
        //  // return
        // //没有淘汰附加赛
        // if (battle.isno == 0) {
        //   if (wheel == maxWheel) wheelName = '决赛'
        //   if (wheel == (parseInt(maxWheel) - 1)) wheelName = '半决赛'
        // }
        // // 有淘汰附加赛
        // else if(battle.isno == 1){
        //   // 最后一轮
        //   if (wheel == parseInt(maxWheel) && field == 1) wheelName = '决赛'
        //   if (wheel == parseInt(maxWheel) && field == 2) wheelName = '5-6淘汰附加赛'
        //   if (wheel == parseInt(maxWheel) && field == 3) wheelName = '3-4淘汰附加赛'
        //   if (wheel == parseInt(maxWheel) && field == 4) wheelName = '7-8淘汰附加赛'
        //   // 倒数第二轮
        //   if (wheel == (parseInt(maxWheel) - 1) && (field == 1 || field == 2)) wheelName = '半决赛'
        //   if (wheel == (parseInt(maxWheel) - 1) && (field == 3 || field == 4)) wheelName = '5-8淘汰附加赛'
        // }
        // if (wheel == (parseInt(maxWheel) - 2)) wheelName = '1/4决赛'
        // if (wheel < (parseInt(maxWheel) - 2)) wheelName = `第${battle.wheel}轮`
        // return wheelName
        return battle.wheel_name
    },
    //增加行
    moreField(platoonGrounds) {
        let item = []
            // 横竖加
            // for(let i in this.platoonGrounds){
            //    for(let j in this.platoonGrounds[i]){
            //     item.push({platoonGrounds_id:i,list:[]})
            //   }
            // }
        let length = platoonGrounds[0].length
        for (let i = 0; i < length; i++) {
            item.push({ platoonGrounds_id: i, list: [] })
        }
        platoonGrounds.push(item)
        store.commit('setPlatoonGrounds', platoonGrounds)
            // this.PGNumList.push(item);
            // console.log(this.PGNumList)
    },
    //请求列表渲染进playground
    listToPlayGround(list, platoonGrounds, roundsSelected, teamsInfo) { //列表 场地 轮数选择 组别信息
        //this是当前作用域内的变量
        for (let games in list) {
            let col = parseInt(list[games].col) - 1
            let row = parseInt(list[games].row) - 1
            let groundLenth = platoonGrounds.length - 1
            for (let times = row; times > groundLenth; times--) {
                schedule.moreField(platoonGrounds)
                    // console.log('times',times, platoonGrounds.length-1)
            }
            platoonGrounds[row][col].list.push(list[games])
                // console.log(platoonGrounds)
        }
        // schedule.calcRounds(platoonGrounds,roundsSelected,teamsInfo)
    },
    //生成发布上传对象
    makePublishObj(platoonGrounds, date) {
        let uploadArr = []
        let projectId = 0
        let projectName = ''
        let List = JSON.parse(JSON.stringify(platoonGrounds))
            // return
        for (let i in List) {
            for (let j in List[i]) {
                if (List[i][j].list.length == 1) { //多次排战之后判断有问题
                    List[i][j].list[0].row_id = parseInt(i) + 1 //行
                    List[i][j].list[0].col_id = parseInt(j) + 1 //列
                    if (List[i][j].list[0].status == 0) {
                        uploadArr.push(List[i][j].list[0])
                    }
                }
            }
        }
        let uploadObj = {}
        for (let i in uploadArr) {
            uploadObj[i] = {
                id: uploadArr[i].id,
                place: uploadArr[i].row_id + ',' + uploadArr[i].col_id,
                match_date: uploadArr[i].match_date ? uploadArr[i].match_date : date,
                note: uploadArr[i].note
            }
        }
        return uploadObj
    },
    //拿去manual的值生成轮数
    getPlatoonManual(teams, firstRounds, secondRounds, firstRoundsList, secondRoundsList, Info, roundsSelected) {
        return new Promise((resolve, reject) => {
            axios.post('/LineWar/manual', {
                group_id: teams.project_id,
            }, {
                headers: {
                    token: window.sessionStorage.getItem('token')
                }
            }).then((res) => {
                if (res.data.code != 2001) {
                    reject()
                    return
                }
                teams.roundInfo = res.data.data
                teams.rounds = []
                let round1 = []
                let round2 = []
                let round1Selected = []
                let round2Selected = []
                    //第一轮的长度
                for (let i in teams.roundInfo[0]) {
                    if (firstRounds < parseInt(i) + 1) {
                        firstRounds = parseInt(i) + 1
                        if (teams.roundInfo[0].length > firstRoundsList.length) {
                            firstRoundsList.push({})
                        }
                    }
                    //优先级计次器
                    round1Selected.push({
                        count: 0,
                        radio: 0,
                        countLength: teams.roundInfo[0][i].length,
                        disable: false
                    })
                    teams.roundInfo[0].teamLength = firstRounds
                    round1.push(teams.roundInfo[0][i])
                }
                roundsSelected[0].push(round1Selected)
                teams.rounds[0] = round1
                    //第二轮的长度
                for (let i in teams.roundInfo[1]) {
                    if (secondRounds < parseInt(i) + 1) {
                        secondRounds = parseInt(i) + 1
                        if (teams.roundInfo[1].length > secondRoundsList.length) {
                            secondRoundsList.push({})
                        }
                    }
                    //计次器
                    round2Selected.push({
                        count: 0,
                        radio: 0,
                        countLength: teams.roundInfo[1][i].length,
                        disable: false
                    })
                    teams.roundInfo[1].teamsLength = secondRounds
                    round2.push(teams.roundInfo[1][i])
                }
                teams.rounds[1] = round2
                roundsSelected[1].push(round2Selected)
                Info.push(teams)
                resolve()
            })
        })
    },
}

export default schedule
